package com.wuliupai.delivery.wuliupaidelivery.utils;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;

import org.androidannotations.annotations.EBean;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Descriptions：文件操作的工具类
 * <p>
 * Author：ChenME
 * Date：11/14/2016
 * Email：ibelieve1210@163.com
 */
@EBean(scope = EBean.Scope.Singleton)
public class FileUtils {

    /**
     * 项目的根目录
     */
    public static final String APP_FILE_PATH = "/WuliupaiDelivery";
    /**
     * 图片路径
     */
    public final String APP_IMG_PATH = "/img";

    /**
     * 获取SD卡根目录的路径
     *
     * @return SD卡存在，返回路径
     * 不存在，则返回空
     */
    private String getSDCardRootPath() {
        File sdDir = null;
        boolean sdCardExist = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED); //判断sd卡是否存在
        if (sdCardExist) {
            sdDir = Environment.getExternalStorageDirectory();//获取跟目录
            return sdDir.toString();
        }
        return null;
    }

    /**
     * 创建文件夹并返回文件的绝对路径
     *
     * @param dirsPath 文件夹名称
     * @return 文件夹绝对路径
     * @throws Exception SD卡不存在
     */
    public String creatFolderAndReturnPath(String dirsPath) throws IOException {
        String sdCardRootPath = getSDCardRootPath();
        if (null == sdCardRootPath) {
            throw new IOException("Custom Exception —— SD卡不存在");
        }
        File file = new File(sdCardRootPath + APP_FILE_PATH + "/" + dirsPath);
        if (!file.exists()) {
            file.mkdirs();
        }
        return file.getAbsolutePath();
    }

    /**
     * 创建并获取图片的Uri
     * @return 图片Urine
     * @throws IOException
     */
    public Uri getImageUri() throws IOException {
        Uri uri = null;
        String imgName = "Notes_" + new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()) + ".jpg";
        File file = new File(creatFolderAndReturnPath(APP_IMG_PATH), imgName);
        if (!file.exists()) {
            file.createNewFile();
        }
        uri = Uri.fromFile(file);
        return uri;
    }

    /**
     * 通过 uri 获取路径 path
     *
     * */
    public String getRealFilePath(final Context context, final Uri uri) {
        if (null == uri)
            return null;
        final String scheme = uri.getScheme();
        String data = null;
        if (scheme == null)
            data = uri.getPath();
        else if (ContentResolver.SCHEME_FILE.equals(scheme)) {
            data = uri.getPath();
        } else if (ContentResolver.SCHEME_CONTENT.equals(scheme)) {
            Cursor cursor = context.getContentResolver().query(uri,
                    new String[]{MediaStore.Images.ImageColumns.DATA}, null, null, null);
            if (null != cursor) {
                if (cursor.moveToFirst()) {
                    int index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
                    if (index > -1) {
                        data = cursor.getString(index);
                    }
                }
                cursor.close();
            }
        }
        return data;
    }

    /**
     * 删除单个文件
     * @param   sPath    被删除文件的文件名
     * @return 单个文件删除成功返回true，否则返回false
     */
    public static boolean deleteFile(String sPath) {
        boolean   flag = false;
        File file = new File(sPath);
        // 路径为文件且不为空则进行删除
        if (file.isFile() && file.exists()) {
            file.delete();
            flag = true;
        }
        return flag;
    }

}